<?php

namespace StudyBuddy\Controllers;

use StudyBuddy\Responder;

use StudyBuddy\WebPage;
use StudyBuddy\Paginator;
use StudyBuddy\Template\Urhere;
use StudyBuddy\Request;


/**
 * Controller for rendering
 * the "Members" page
 *
 * If request is by Ajax, it returns only the content
 * of the main area, paginated, sorted and with pagination
 * links if necessary
 *
 */
class Managebadges extends WebPage
{

	protected $layoutID = 1;

	protected $qtab = 'questions';

	protected $oCursor;

	protected $badgesCount;

	protected $Html;
	
	protected $curtab;
	
	protected $permission = 'delete_answer';

	protected function main(){

		$this->init()
		->getCursor()
		->renderHtml();

		$this->setTitle()
		->makeTopTabs()
		//->makeBadgesLegen()
		->setUsers();
	}


	
	protected function setTitle(){
		$title = 'Manage Badges';
		$this->aPageVars['title'] = $title;
		$this->aPageVars['qheader'] = '<h1>'.$title.'</h1>';

		return $this;
	}

	protected function init(){
	
		$this->getUser();

		$this->sortOrder = array('_id' => 1);

		return $this;
	}

	protected function getUser(){
		
		$uid = $this->oRequest->get('uid', 'i', null);
		if($uid && ($uid !== $this->oRegistry->Viewer->getUid())){
			/**
			 * This is edit tutor for another user
			 * check Viewer permission here
			 */
			$this->checkAccessPermission($permission);
			$this->oUser = \StudyBuddy\User::factory($this->oRegistry)->by_id($uid);
		} else {
			$this->oUser = $this->oRegistry->Viewer;
		}

		return $this;
	}
	
	protected function makeTopTabs(){
            $guest = null;
            if($this->oRegistry->Viewer->isGuest()) {
                $guest['link_ask'] = '#'; // 13
                $guest['link_ask_click'] = ' onclick="oSL.getQuickRegForm()"'; //14
            }
		d('cp');
		$tabs = Urhere::factory($this->oRegistry)->get('tplToptabs', $this->qtab, $guest);
		$this->aPageVars['topTabs'] = $tabs;

		return $this;
	}


	protected function getCursor(){
	
		$this->curtab = $this->oRegistry->Request->get('tab', 's', 'all');
		$where = array('type' => $this->curtab);
		
		if($this->curtab == 'all'){
			$this->oCursor = $this->oRegistry->Mongo->BADGES ->find();
		}else{
			$this->oCursor = $this->oRegistry->Mongo->BADGES ->find($where);
		}
		$this->oCursor->sort($this->sortOrder);
		$this->badgesCount = $this->oCursor->count();

		return $this;
	}
	
	//hoangto tutor listing
	protected function makeBadgesLegen(){

		$this->aPageVars['badges_legend'] = '
			<div id="badge-legend">
				<div class="mb">
					<a class="badge" title="gold badge: awarded rarely" href="/badges/gold"><span class="gold">&#9679;</span>&nbsp;Gold Badge</a>
				</div>
				<p>Gold Badges are rare. You\'ll have to actively work toward these. They\'re something of an accomplishment!</p>
				<div class="mb">
					<a class="badge" title="silver badge: awarded occasionally" href="/badges/silver"><span class="silver">&#9679;</span>&nbsp;Silver Badge</a>
				</div>
				<p>Silver badges are awarded for longer term goals. Silver badges are uncommon, but definitely attainable if you\'re interested.</p>
				<div class="mb">
					<a class="badge" title="bronze badge: awarded frequently" href="/badges/bronze"><span class="bronze">&#9679;</span>&nbsp;Bronze Badge</a>
				</div>
				<p>Bronze badges are awarded for basic use of Study Buddy. They are easy to earn.</p>
			</div>';

		return $this;
	}

	protected function renderHtml(){
		
		$curbronze = '';
		$cursilver = '';
		$curgold = '';
		$curall = '';
		
		switch ($this->curtab) {
			case 'bronze':
				$curbronze = 'class="youarehere"';
				break;
			case 'gold':
				$curgold = 'class="youarehere"';
				break;
			case 'silver':
				$cursilver = 'class="youarehere"';
				break;
			default:
				$curall = 'class="youarehere"';
		}
		
		$this->Html =	'<div class="ctl-ct badgeswrap">
						<h3>Manage Badges</h3>
						<div class="subtabs" id="tabs-type">
							<a title="silver badges" href="/managebadges/bronze" '.$curbronze.'>bronze</a>
							<a title="silver badges" href="/managebadges/silver" '.$cursilver.'>silver</a>
							<a title="gold badges" href="/managebadges/gold" '.$curgold.'>gold</a>
							<a title="all badges" href="/managebadges" '.$curall.'>all</a>
						</div>
                                                <div style=" float: right;padding-right: 25px;padding-top: 10px;text-align: right;width: 100%;">
                                                    <a href="/addbadges/" style="font-weight:bold;">Add Badge</a>
                                                </div>  
						';
		$this->Html .= '<div style="padding:50px;"><table>';
		
		foreach($this->oCursor as $obj){
			$coll = $this->oRegistry->Mongo->USER_BADGES->find(array('badges' => $obj['b_name']));
			
			$this->Html .=	'<tr>
                                            <td class="check-cell">
                                                <a href="/editbadges/'.$obj['id'].'" style="text-decoration: none;">
                                                    <span class="editbadges" title="Edit">edit</span>
                                                </a>
                                                <a href="/deletebadges/'.$obj['id'].'" onclick="return confirm(\'Are you sure you want to delete this badge ?\') "style="text-decoration: none;">
                                                    <span class="deletebadges" title="Edit">delete</span>
                                                </a>
                                            </td>
                                            <td class="badge-cell" width="200px">
                                                <a class="badge" title="'.$obj['b_desc'].'" href="/badges/'.$obj['id'].'">
                                                <span class="'.$obj['type'].'">&#9679;</span>&nbsp;'.$obj['b_name'].'</a>
                                                <span class="item-multiplier">x&nbsp;'.$coll->count().'</span>
                                            </td>
                                            <td>'.$obj['b_desc'].'</td>
                                        </tr>';
		}
		$this->Html .= '</table></div>  
                                </div>';

		return $this;
	}


	/**
	 * Adds the content of users block
	 * to page body
	 *
	 * @return object $this
	 */
	protected function setUsers(){
	
		$this->aPageVars['body'] .= $this->Html;

		return $this;
	}

}
